package com.atguigu.yygh.host.controller;

import com.atguigu.yygh.common.R;
import com.atguigu.yygh.host.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author DongYu Zhou
 * @date 2022/6/16 14:49
 */
//医院设置接口
@Api(description = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //*login（post） ： {"code":20000,"data":{"token":"admin-token"}}
    @ApiOperation(value = "模拟登录")
    @PostMapping("/login")
    public R login(){
        return R.ok().data("token","admin-token");
    }

    //*info （get）：
    //{"code":20000,"data":{"roles":["admin"],
    //"introduction":"I am a super administrator",
    //"avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
    //"name":"Super Admin"}}
    @ApiOperation(value = "模拟获取用户信息")
    @GetMapping("/info")
    public R info(){
        HashMap<String, Object> map = new HashMap<>();

        map.put("roles","admin");
        map.put("introduction","I am a super administrator");
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("name","Super Admin周冬雨");

        return R.ok().data(map);
    }

    //查询所有医院设置
    @ApiOperation(value = "医院设置列表")
    @GetMapping("findAll")
    public R findAll() {
//        try {
//            int num= 10/0;
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw  new YyghException(20001,"自定义异常");
//        }
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list", list);
    }

    //删除信息
    @ApiOperation(value = "医院设置删除")
    @DeleteMapping("{id}")
    public R delHospSet(@PathVariable Long id) {
        hospitalSetService.removeById(id);
        return R.ok();
    }


    /**
     * <img src="http://re1gghye1.hn-bkt.clouddn.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20220625225617.jpg" height="250" width="250">
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "分页查询医院设置")
    @GetMapping("/pageQuery/{page}/{limit}")
    public R pageQuery(@PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Page<HospitalSet> hospitalSetPage = new Page<>(page, limit);
        Page<HospitalSet> setPage = hospitalSetService.page(hospitalSetPage);
        long total = hospitalSetPage.getTotal();
        return R.ok().data("setPage", setPage).data("total", total);

    }

    @ApiOperation(value = "带条件分页查询医院设置")
    @PostMapping("/pageInfo/{page}/{limit}")
    public R pageInfo(@PathVariable("page") Long page,
                      @PathVariable("limit") Long limit,
                      @RequestBody HospitalSetQueryVo hospitalSetQueryVo) {

        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hoscode);
        }
        Page<HospitalSet> hospitalSetPage = new Page<>(page, limit);
        Page<HospitalSet> setPage = hospitalSetService.page(hospitalSetPage, wrapper);
        long total = hospitalSetPage.getTotal();
        return R.ok().data("setPage", setPage).data("total", total);

    }

    @ApiOperation(value = "添加医院设置")
    @PostMapping("/addHostSet")
    public R addHostSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping("/getHospSetById/{id}")
    public R selHostSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        return R.ok().data("hospitalSet", hospitalSet);
    }

    @ApiOperation(value = "id修改医院设置")
    @PostMapping("/updateHospSet") //传过来的实体有id
    public R updateHospSet(@RequestBody HospitalSet hospitalSet) {
        boolean update = hospitalSetService.updateById(hospitalSet);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestBody List<Long> listIds) {
        boolean batch = hospitalSetService.removeBatchByIds(listIds);
        if (batch) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 医院设置锁定和解锁
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id, @PathVariable Integer status) {

        //先查询在修改
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //修改
        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }


}
